//评论
var getComments;
require(['grid', 'topBar', 'date', 'highcharts'], function () {
    //提交到后台参数
    var postData;
    $('#top').topBar({
        left: [
            {
                type: 'filter',
                search: function () {
                    search();
                },
                item: [
                    {
                        text: '时间快捷',
                        name: 'reportType',
                        targetId: 'beginTime',
                        type: 'reportDateSelect'
                    }, {
                        text: '查询时间',
                        id: 'beginTime',
                        name: 'beginTime',
                        type: 'text',
                        width: 250
                    }
                ]
            }
        ],
        right: [{
            text: '导出',
            type: 'export-btn',
            authority: '',
            item: [{
                text: 'Excel导出',
                click: function () {
                    exportFile('Excel');
                }
            }, {
                text: 'PDF导出',
                click: function () {
                    exportFile('Pdf');
                }
            }]
        }]
    });
    function exportFile(suffix) {
        if ($('#grid').getGridParam("reccount") < 1) {
            $.messager.alert("提示", "请先查询数据。", "info");
            return;
        }
        if (getPostData()) {
            var url = "/securityBehaviour/exportSecurityBhv" + suffix + ".htm";
            $.exportFile(url, postData);
        }
    }

    $('#grid').parent().height($(window).height() - 173);

    getComments = function (rowObject) {
        if (rowObject.allRank == '-') {
            $("#showSafeMsg").css("display", "none");
        } else {
            var param = $('#top').parent().getValues();
            param.drvName = rowObject.drvName;
            param.lineName = rowObject.lineName;
            param.organName = rowObject.organName;
            param.compareValue = rowObject.totalGrade;
            param.lineId = rowObject.lineId;
            param.safeCount1 = parseInt(rowObject.summary1) + parseInt(rowObject.summary2) + parseInt(rowObject.summary3);
            param.safeCount2 = rowObject.summary4;
            $.ajaxExt({
                url: "/securityBehaviour/getSecurityComments.htm",
                data: param
            }).done(function (result) {
                if (result[1] == "优秀" || result[1] == "良好") {
                    $("#securityTitle").html("<font style = 'color:green;'>" + result[1] + "</font>");
                } else if (dataStr[1] == "合格" || dataStr[1] == "基本合格") {
                    $("#securityTitle").html("<font style = 'color:orange;'>" + result[1] + "</font>");
                } else {
                    $("#securityTitle").html("<font style = 'color:red;'>" + result[1] + "</font>");
                }
                $("#securityComments").html("<b>评语:</b>" + result[0]);
                $("#securityDrvName").html(rowObject.drvName);
                $("#securityScore").html(rowObject.totalGrade);
                var totalGrade = parseInt(rowObject.summary1) + parseInt(rowObject.summary2) + parseInt(rowObject.summary3) + parseInt(rowObject.summary4);
                $("#securityCount").html(totalGrade);
            });
            $("#showSecurityMsg").css("display", "block");
        }
    }

    $('#grid').grid({
        multiselect: false,
        url: "/securityBehaviour/searchSecurityBehaviour.htm",
        colModel: [{name: "lineId", label: "", hidden: true},
            {name: "allRank", label: "排名", width: 40, align: "right"},
            {name: "accdtGrade", label: "事故得分", width: 80, align: "right", formatter: 'customCurrency'},
            {name: "illegalGrade", label: "违章得分", width: 80, align: "right"},
            {name: "totalGrade", label: "总得分", width: 80, align: "right"},
            {name: "drvName", label: "驾驶员", width: 80},
            {name: "lineName", label: "运营线路", width: 80},
            {name: "organName", label: "所属机构", width: 80},
            {name: "totalDura", label: "运行时长", align: "right", width: 80},
            {name: "totalMile", label: "运行里程(KM)", align: "right", formatter: 'customCurrency', width: 120},
            {name: "summary1", label: "<300", align: "right", formatter: 'customCurrency', width: 80},
            {name: "summary2", label: "300-1000", align: "right", formatter: 'customCurrency', width: 80},
            {name: "summary3", label: ">1000", align: "right", formatter: 'customCurrency', width: 80},
            {name: "summary4", label: "违章次数", align: "right", formatter: 'customCurrency', width: 80},
            {name: "summary5", label: "夜间行车", align: "right", formatter: 'customCurrency', width: 80},
            {name: "summary6", label: "起步急加", align: "right", formatter: 'customCurrency', width: 80},
            {name: "summary7", label: "超速", align: "right", formatter: 'customCurrency', width: 80}
        ],
        onSelectRow: function (ids) {
            var data = $('#grid').getRowData(ids);
            getComments(data);
        }
    });

    $('#grid').jqGrid('setGroupHeaders', {
        useColSpanStyle: true,
        groupHeaders: [{
            startColumnName: 'summary1',
            numberOfColumns: 3,
            align: 'center',
            titleText: "<b>事故次数</b>"
        },
            {
                startColumnName: 'accdtGrade',
                numberOfColumns: 3,
                titleText: "<b>得分</b>"
            }]
    });

    function search() {
        if (getPostData()) {
            $('#grid').grid('setGridParam', {
                page: 1,
                datatype: "json",
                postData: postData
            }).trigger("reloadGrid");
        }
    }

    function getPostData() {
        postData = $('#top').parent().getValues();
        if(!postData.beginTime){
            $.messager.alert("提示", "请先选择查询时间。", "info");
            return false;
        }
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if (nodes.LINE) {
            postData.orgIds = nodes.LINE;
            postData.type = "LINE";
            return true;
        }
        $.messager.alert("提示", "至少请选择一条线路。", "info");
        return false;
    }

});
